/*
*
*在main.php中加入“<div id="append_parent"></div><div id="append_box"></div><div id="ajaxwaitid"></div>”
*@param e:event(事件);ctrlid:事件id;timeout:消失时间默认为0(不消失);bg:是否有背景(默认为有);
*eg:<a href="index.php?r=site/test" id ="test" onclick="ajaxbox(event,this.id);" >test</a>
*href是ajax请求地址
*
*/
var jsbox = new Array();
var ctrlobjclassname;
jsbox['active']=new Array();
jsbox['timer'] = new Array();
jsbox['iframe'] = new Array();

function msgbox(e,ctrlid,msg,timeout,bg){
	var offset = 0;
	var duration = 3;
	if(isUndefined(timeout)) timeout = 0;
	if(isUndefined(bg)) bg = true;
		showloading();
	if(jsbox['active'][0]&&jsbox['active'][0].ctrlkey==ctrlid){
		hideBox();
		doane(e);
		showloading('none');
		return;
	}else if(is_ie&&is_ie<7&&document.readyState.toLowerCase()!='complete'){
		return;
	}
		divclass='popupbox_centerbox';
		offset = -1;
	if(bg){
		$("#append_parent").css({
			"position":"absolute",
			"top":"0",
			"left":"0",
			"width":document.body.clientWidth+'px',
			"height":document.body.clientHeight+'px',
			"z-index":"40",
			"background-color":"#FCF9E6",
			"filter":"alpha(opacity=50)",
			"-moz-opacity":"0.5",
			"opacity":"0.5"
		});
	}
	var div = document.getElementById(ctrlid+'_box');
	if(!div){
		$("#append_box").append('<div id="'+ctrlid+'_box" style="display:none" class="'+divclass+'"></div>');
	}
	/*var href = !isUndefined($("#"+ctrlid).href) ? $("#"+ctrlid).href : $("#"+ctrlid).attr('href');
	var options = {
		url:href,
		fun:function(){
			var html = this.responseText;
			var html = '<h1>消息</h1><a href="javascript:hideBox();" class="float_del" title="关闭">关闭</a><div class="popupboox_inner">' + html + '<div>';
			$("#"+ctrlid+"_box").html(html);
			showBox(ctrlid,e.type=='click',offset,duration,timeout,0,ctrlid,1000);
		},
		method:'GET'
	}
	var request = createRequest(options);
	request.send(null);*/
	var html = '<h1>消息</h1><a href="javascript:hideBox();" class="float_del" title="关闭">关闭</a><div class="popupboox_inner">' + msg + '<div>';
	$("#"+ctrlid+"_box").html(html);
	showloading('none');
	doane(e);
}

function showBox(ctrlid,click,offset,duration,timeout,layer,showid,maxh,drag){
	var ctrlobj = $("#"+ctrlid);
	if (!ctrlobj) return;
	if(isUndefined(click)) click=false;
	if(isUndefined(offset)) offset = 0;
	if(isUndefined(duration)) duration = 2;
	if(isUndefined(timeout)) timeout = 250;
	if(isUndefined(layer)) layer = 0;
	if(isUndefined(showid)) showid = ctrlid;
	var showobj = $("#"+showid);
	var boxobj = $("#"+showid+'_box');
	if(!showobj||!boxobj)return;
	if(isUndefined(maxh)) maxh = 1000;
	if(isUndefined(drag)) drag = false;

	if(click && jsbox['active'][layer] == boxobj){
		hideBox(layer);
		return;
	}else{
		hideBox(layer);	
	}

	var len = jsbox['timer'].length;
	if(len>0){
		for(var i=0;i<len;i++){
			if(jsbox['timer'][i]) clearTimeout(jsbox['timer'][i]);
		}
	}

	initCtrl(ctrlobj,click,duration,timeout,layer);
	ctrlobjclassName = ctrlobj.className;
	ctrlobj.className += ' hover';
	initbox(ctrlid,boxobj,duration,timeout,layer,drag);
	boxobj.show();
	if(!is_opera) {
		boxobj.css("clip","rect(auto, auto, auto, auto)");
	}
	/*box position*/
	if(showobj) {
		showobj.pos = fetchOffset(showobj);
		showobj.X = showobj.pos['left'];
		showobj.Y = showobj.pos['top'];
		showobj.w = showobj.width();
		showobj.h = showobj.height();
		boxobj.w = boxobj.width();
		boxobj.h = boxobj.height();
		var sTop = document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		if(offset != -1) {
			boxobj.scrollLeft((showobj.X + boxobj.w > document.body.clientWidth) && (showobj.X + showobj.w - boxobj.w >= 0) ? showobj.X + showobj.w - boxobj.w + 'px' : showobj.X + 'px');
			boxobj.scrollTop(offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + boxobj.h > sTop + document.documentElement.clientHeight) && (showobj.Y - boxobj.h >= 0)) ? (showobj.Y - boxobj.h) + 'px' : showobj.Y + showobj.h + 'px'));
		} else if(offset == -1) {
			var divtop = sTop + (document.documentElement.clientHeight-boxobj.h)/2;
			if(divtop > 100) divtop = divtop - 100;
			
			boxobj.css({
				'left':($(window).width()-boxobj.w)/2 + 'px',
				'top':($(window).height()-boxobj.h)/2 + 'px'
			});
		}
		if(boxobj.css("clip") && !is_opera) {
			boxobj.css("clip","rect(auto, auto, auto, auto)");
		}
	}
	/*position end*/
	if(maxh && boxobj.height > maxh) {
		boxobj.height = maxh + 'px';
		if(is_opera) {
			boxobj.css("overflow","auto");
		} else {
			boxobj.css("overflowY","auto");
		}
	}

	if(!duration) {
		setTimeout('hidebox(' + layer + ')', timeout);
	}

	jsbox['active'][layer] = boxobj;
}

function initbox(ctrlid,boxobj,duration,timeout,layer,drag){
	if(boxobj&&!boxobj.initialized){
		boxobj.initialized = true;
		boxobj.ctrlkey = ctrlid;
		boxobj.onclick = ebygum;

		if(is_ie&&is_ie<7){
			boxobj.css({"position":"absolute"});
		}else{
			boxobj.css({"position":"fixed"});
		}
		if(duration<3){
			if(duration>1){
				boxobj.onmouseover = function(){
					clearTimeout(jsbox['timer'][ctrlid]);
				}
			}
			if(duration!=1){
				boxobj.onmouseout = function(){
					jsbox['timer'][ctrlid] = setTimeout('hideBox('+layer+')',timeout);
				}
			}
		}
		boxobj.css({"z-index":"50","width":"300px"});
		if(is_ie) {
			boxobj.css("filter","progid:DXImageTransform.Microsoft.shadow(direction=135,color=#CCCCCC,strength=2)");
		}
		if(drag) {
			boxobj.mousedown(function(event) {try{boxdrag(boxobj, event, 1);}catch(e){}});
			boxobj.mousemove(function(event){try{boxdrag(boxobj,event,2);}catch(e){}});
			boxobj.mouseup(function(event) {try{boxdrag(boxobj, event, 3);}catch(e){}});
		}
	}
}
function ebygum(eventobj) {
	if(!eventobj || is_ie) {
		window.event.cancelBubble = true;
		return window.event;
	} else {
		if(eventobj.target.type == 'submit') {
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}
var boxdragstart = new Array();
function boxdrag(boxobj, e, op){
	if(op == 1) {
		boxdragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		boxdragstart[2] = parseInt(boxobj.css("left"));
		boxdragstart[3] = parseInt(boxobj.css("top"));
		doane(e);
	} else if(op == 2 && boxdragstart[0]) {
		var boxdragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		boxobj.css("left",(boxdragstart[2] + boxdragnow[0] - boxdragstart[0]) + 'px');
		boxobj.css("top",(boxdragstart[3] + boxdragnow[1] - boxdragstart[1]) + 'px');
		doane(e);
	} else if(op == 3) {
		boxdragstart = [];
		doane(e);
	}
}
function initCtrl(ctrlobj,click,duration,timeout,layer){
	if(ctrlobj&&!ctrlobj.initialized){
		ctrlobj.initialized = true;
		ctrlobj.unselectable = true;

		ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function'?ctrlobj.onmouseout:null;
		ctrlobj.onmouseout = function(){
			if(this.outfunc) this.outfunc();
			if(duration<3)jsbox['timer'][ctrlobj.id] = setTimeout('hideBox('+layer+')',timeout);
		}

		ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function'?ctrlobj.onmouseover:null;
		ctrlobj.onmouseover = function(e){
			doane(e);//jquery函数阻止冒泡事件
			if(this.overfunc) this.overfunc();
			if(click){
				clearTimeout(jsbox['timer'][this.id]);
			}else{
				for(var id in jsbox['timer']){
					if(jsbox['timer'][id]) clearTimeout(jsbox['timer'][id]);
				}
			}
		}
	}
}

function hideBox(layer){
	if(isUndefined(layer)) layer = 0;
	if(jsbox['active'][layer]){
		try{
			$("#"+jsbox['active'][layer].ctrlkey.className) = ctrlobjclassName;
		}catch(e){}
		clearTimeout(jsbox['timer'][jsbox['active'][layer].ctrlkey]);
		jsbox['active'][layer].css("display","none");
		if(is_ie&&is_ie<7&&jsbox['iframe'][layer]){
			jsbox['iframe'][layer].css("display","none");
		}
		$("#append_parent").css({
			"position":"",
			"width":'',
			"height":'',
			"z-index":"",
			"background-color":"",
			"filter":"",
			"-moz-opacity":""
		});
		jsbox['active'][layer] = null;
	}
}


function isUndefined(varible){
	return typeof varible=='undefined'?true:false;
}



function fetchOffset(obj) {
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
}